class Solution(object):
    def maximumAlternatingSubarraySum(self, nums):
        n = len(nums)
        if n == 1:
            return nums[0]
        max_val = - float('inf')
        last1 = nums[0] - nums[1]
        last2 = nums[1]
        max_val = max(max_val, last1, last2, nums[0])
        for i in range(2, n):
            last1, last2 = last2 - nums[i], max(last1 + nums[i], nums[i])
            max_val = max(max_val, last1, last2)
        return max_val